package cn.com.fssg.platform.base.handler.impl.cl.tongji;

import cn.com.fssg.platform.base.handler.InterfaceHandler;
import cn.com.fssg.platform.biz.BizUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @className: Cldyq
 * @description: TODO 代用券
 * @author: xzm
 * @date: 2025/10/6
 **/
@Component
public class ClNewTodoCounterInterfaceHandler implements InterfaceHandler {


//    private NamedParameterJdbcTemplate jdbcTemplate = BizUtil.cur().tenantJdbcTemplate("CJRLHHConnString");
    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object run(HttpServletRequest request, JSONObject jsonObject) throws Exception {
        String flag, sql, userId;
        try {
            flag = jsonObject.getString("flag");
            userId = BizUtil.cur().curUserId();
            if (ObjectUtils.isEmpty(flag)) {
                return null;
            }
        } catch (Exception e) {
            return "获取参数失败：" + e.getMessage();
        }
        try {
            sql = getSql(Integer.parseInt(flag), userId);
        } catch (Exception e) {
            return "转换异常：" + e.getMessage();
        }
        try {
            Map<String, Object> map = new HashMap<>();
            return jdbcTemplate.queryForMap(sql, map);
        } catch (Exception e) {
            return "查询异常, sql = " + sql + "; jdbcTemplate:" + jdbcTemplate;
        }
    }

    private String getSql(int flag, String userId) {
        String sql = null;
//        String userId = BizUtil.cur().curUserId();
//        String userId = "755464857003614208";
        switch (flag) {
            case 1:  //康复训练待办统计
                sql = String.format(
                        "SELECT count(1) " +
                                " FROM " +
                                "  pxone.actex_re_task  task" +
                                " left JOIN " +
                                "  pxone.actex_hi_procinst pi on pi.id = task.proc_inst_id" +
                                " left join " +
                                "  cl_apply_xunlian form" +
                                " on " +
                                "  task.biz_key = form.id" +
                                " where " +
                                "  case when task.assignee is not null then task.assignee = '%s'" +
                                "  else candidate = '%s' end" +
                                " AND" +
                                "  task.proc_def_key = 'WF20250915095339'" +
                                " AND" +
                                "  form.shenpi_no is not null",
                        userId, userId
                );
                break;
            case 2:  //辅具适配待办统计
                sql = String.format(
                        "SELECT count(1) " +
                                " FROM " +
                                "  pxone.actex_re_task  task" +
                                " left JOIN " +
                                "  pxone.actex_hi_procinst pi on pi.id = task.proc_inst_id" +
                                " left join " +
                                "  cl_apply_fuju form" +
                                " on " +
                                "  task.biz_key = form.id" +
                                " where " +
                                "  case when task.assignee is not null then task.assignee = '%s'" +
                                "  else candidate = '%s' end" +
                                " AND" +
                                "  task.proc_def_key = 'WF20250928183406'" +
                                " and" +
                                "  form.shenpi_no is not null",
                        userId, userId
                );
                break;
            case 3:  //结算申请待办统计
                sql = String.format(
                        "SELECT" +

                                "  count(COALESCE (t1.cjr_name, t2.cjr_name)) " +
                                " FROM" +
                                "  pxone.actex_re_task task " +
                                " LEFT JOIN pxone.actex_hi_procinst pi on pi.id = task.proc_inst_id " +
                                " LEFT JOIN cl_apply_xunlian_org_jiesuan t1 ON task.biz_key = t1.id " +
                                " LEFT JOIN cl_apply_fuju_org_jiesuan t2 ON task.biz_key = t2.id " +
                                " WHERE" +
                                "  case when task.assignee is not null then task.assignee = '%s'" +
                                "  else candidate = '%s' end " +
                                " AND" +
                                "  pi.process_page_id is not null " +
                                " and" +
                                "  COALESCE (t1.shenpi_no, t2.shenpi_no) IS NOT NULL " +
                                " and task.proc_def_key in ('WF20251008151332', 'WF20250923202904')",
                        userId, userId
                );
                break;
            default:
                break;
        }
        return sql;
    }
}